home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c,comp.lang.c.moderated
- Subject: Re: const pointer confusion...
- Date: 25 Mar 1996 06:21:56 -0600
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4j6354$3ge@solutions.solon.com>
- References: <4j06gm$7oa@solutions.solon.com> <4j41hs$nku@solutions.solon.com>
- NNTP-Posting-Host: solutions.solon.com
-
-
- The Amorphous Mass <robinson@blue.weeg.uiowa.edu> writes:
-
- >On 23 Mar 1996, Reed R. Mangino wrote:
-
- >> Could someone please straighten me out on this:
- >>
- >> 1) const int *p = 10;
- >> p is a constant pointer to an int, right? While p can be made to
- >> point to something else, *p can never be assigned to, right?
-
- > No, p is a pointer to a constant integer. However, you are right that
- >*p cannot change, but p can.
-
- >> 2) int *const p;
- >> p is a pointer to an integer. *p can be assigned to, but p can
- >> never be made to point to another address in memory, right?
-
- > This is a syntax error.
-
- nausikaa:/home/kurt/src> cc cip.c
- nausikaa:/home/kurt/src> ./a.out
- 42
- nausikaa:/home/kurt/src> cat cip.c
- #include <stdio.h>
-
- int main()
- {
- int i = 42;
- int * const p = &i;
-
- printf("%d\n", *p);
- return 0;
- }
-
- Should I complain to my vendor? Should I complain to the vendors of
- all compilers that accept this code?
-
- >> 3) int const *p;
- >> What the heck is this? I can't find anything like this in my
- >> books, but my compiler thinks everything is hunky doory!???
-
- > This is a constant pointer to an integer. *p can be changed, but p
- >cannot.
-
- nausikaa:/home/kurt/src> cc icp.c
- nausikaa:/home/kurt/src> ./a.out
- effffc4c
- nausikaa:/home/kurt/src> cat icp.c
- #include <stdio.h>
-
- int main()
- {
- const int i = 42;
- int const *p;
-
- p = &i;
- printf("%p\n", (void *)(++p));
- return 0;
- }
-
- Should I complain to the compiler vendor again? Obviously this compiler
- accepts changes to "p".
-
- AFAIK, "int const *p;" tells us that "p" is a pointer to a constant int.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-
- > Also, there's
-
- > 4) const int const *p;
-
- > Which is a constant pointer to a constant integer; neither *p nor
- >p can change.
-
- nausikaa:/home/kurt/src> cc cicp.c
- "cicp.c", line 6: invalid type combination
- cc: acomp failed for cicp.c
- nausikaa:/home/kurt/src> cat cicp.c
- #include <stdio.h>
-
- int main()
- {
- int i = 42;
- const int const *p = &i;
-
- printf("%d\n", *p);
- return 0;
- }
-
- I think I'd better get a new compiler, if it cannot compile such a
- simple program.
-
- AFAIK, "const int const *p;" _indeed is_ a syntax error.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-